package com.devenv.repository;

import com.devenv.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 用户数据访问层
 * <p>
 * 提供用户实体的数据库访问操作，包括基础CRUD、条件查询、统计查询等
 * </p>
 * 
 * @author DevEnv OpenSource Team
 * @version 1.0.0
 * @since 1.0.0
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    
    /**
     * 根据用户名查找用户
     */
    Optional<User> findByUsername(String username);
    
    /**
     * 根据邮箱查找用户
     */
    Optional<User> findByEmail(String email);
    
    /**
     * 检查用户名是否存在
     */
    boolean existsByUsername(String username);
    
    /**
     * 检查邮箱是否存在
     */
    boolean existsByEmail(String email);
    
    /**
     * 根据密码重置令牌查找用户
     */
    Optional<User> findByPasswordResetToken(String passwordResetToken);
    
    /**
     * 根据状态查找用户
     */
    List<User> findByStatus(Integer status);
    
    /**
     * 根据角色查找用户
     */
    List<User> findByRole(String role);
    
    /**
     * 根据登录类型查找用户
     */
    List<User> findByLoginType(String loginType);
    
    /**
     * 查找最近活跃的用户
     */
    @Query("SELECT u FROM User u WHERE u.lastActiveTime > :since ORDER BY u.lastActiveTime DESC")
    List<User> findRecentActiveUsers(@Param("since") LocalDateTime since);
    
    /**
     * 查找注册时间在指定范围内的用户
     */
    @Query("SELECT u FROM User u WHERE u.createdAt BETWEEN :startTime AND :endTime")
    List<User> findUsersByRegistrationTime(@Param("startTime") LocalDateTime startTime, 
                                          @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计用户总数
     */
    @Query("SELECT COUNT(u) FROM User u WHERE u.status = 1")
    long countActiveUsers();
    
    /**
     * 统计今日新注册用户数
     */
    @Query("SELECT COUNT(u) FROM User u WHERE DATE(u.createdAt) = CURRENT_DATE")
    long countTodayRegistrations();
    
    /**
     * 根据邮箱验证状态查找用户
     */
    List<User> findByEmailVerified(Boolean emailVerified);
    
    /**
     * 根据手机验证状态查找用户
     */
    List<User> findByPhoneVerified(Boolean phoneVerified);
    
    /**
     * 分页查询用户，支持按昵称模糊搜索
     */
    @Query("SELECT u FROM User u WHERE u.nickname LIKE %:keyword% OR u.username LIKE %:keyword% OR u.email LIKE %:keyword%")
    Page<User> findByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 查找资料完整度低于指定值的用户
     */
    @Query("SELECT u FROM User u WHERE u.profileCompletion < :threshold")
    List<User> findUsersWithLowProfileCompletion(@Param("threshold") java.math.BigDecimal threshold);
    
    /**
     * 查找长时间未登录的用户
     */
    @Query("SELECT u FROM User u WHERE u.lastLoginTime < :threshold OR u.lastLoginTime IS NULL")
    List<User> findInactiveUsers(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 根据脚本生成次数排序查找用户
     */
    @Query("SELECT u FROM User u ORDER BY u.scriptGenerationCount DESC")
    Page<User> findUsersOrderByScriptGeneration(Pageable pageable);
    
    /**
     * 根据模板创建次数排序查找用户
     */
    @Query("SELECT u FROM User u ORDER BY u.templateCreationCount DESC")
    Page<User> findUsersOrderByTemplateCreation(Pageable pageable);

    /**
     * 根据用户名或邮箱搜索用户
     */
    Page<User> findByUsernameContainingOrEmailContaining(String username, String email, Pageable pageable);
}
